package chances.epg.play.ctrl;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import chances.epg.commons.log.EpgLogFactory;
import chances.epg.commons.utils.IPUtils;
import chances.epg.entity.biz.Biz;
import chances.epg.navigator.AccessException;
import chances.epg.navigator.context.AccessContext;
import chances.epg.navigator.controller.AccessController;
import chances.epg.navigator.service.NavigateUrlFactory;
import chances.epg.session.UserSession;
import chances.epg.session.UserSessionHelper;
import chances.epg.session.UserSessionService;

@Controller

public class PlayController extends AccessController {

	final static Logger playerLogger = EpgLogFactory.getPlaylogger();

	final static Logger playEndLogger = EpgLogFactory.getPlayendlogger();
	
	final static Logger sysLogger = EpgLogFactory.getSystemLogger();

	@Autowired
	UserSessionService userSessionService;

	@RequestMapping(value = "/{siteCode}/{bizCode}/play/{contentType}/{contentCode}")
	public String playInBiz(ModelMap model, HttpServletRequest request) {
		AccessContext context = this.accessContextFactory.createAccessContext(request);
		return this.play(model, request, context);
	}

	@RequestMapping(value = "/{siteCode}/{bizCode}/play/{parentContentType}/{parentContentCode}/{contentCode}")
	public String playInContainer(ModelMap model, HttpServletRequest request) {
		AccessContext context = this.accessContextFactory.createAccessContext(request);
		if (context.getParentContentType().equals(NavigateUrlFactory.SERIES_TYPE)) {
			context.setContentType(NavigateUrlFactory.SERIES_TYPE);
		}
		return this.play(model, request, context);
	}

	@RequestMapping(value = "/{siteCode}/{bizCode}/playback/{stbNo}")
	public String playBack(ModelMap model, HttpServletRequest request, @PathVariable("stbNo") String userId) {
		UserSession userSession = UserSessionHelper.getUserSession(request);
		AccessContext context = this.accessContextFactory.createAccessContext(request);
		String siteCode = context.getEpgSiteContext().getSiteCode();
		if(userSession == null){
			sysLogger.info("playback Not found Session {}, {}" ,userId,request.getSession().getId() );
		}
		if (userSession == null && userId != null) {
			userSession = this.userSessionService.get(userId);
			if(userSession != null){
				sysLogger.info("playback load session from userSession {} {}" ,userId ,request.getSession().getId() );
				UserSessionHelper.saveUserSessionToSession(request, userSession);
			}
		}
		if (userSession != null) {
			request.setAttribute("userSession", userSession);
			playEndLogger.info("playback|{}|{}|{}|{}|{}|{}", IPUtils.getIpAddr(request), userSession.getUserId(),context.getSiteCode(),
					context.getBizCode(), userSession.getAttribute("content"), request.getQueryString());
		}

		return "/" + siteCode + "/play/play_back.jsp";
	}

	protected String play(ModelMap model, HttpServletRequest request, AccessContext context){
		UserSession userSession = UserSessionHelper.getUserSession(request);
		String playBackUrl = this.navigateUrlFactory.generatePlayBackUrl(context,userSession.getUserId());
		request.setAttribute("playBackUrl",playBackUrl);
		Biz biz = this.getCurBiz(context);
		if (biz == null) {
			logger.error("biz not found,uri:{}", request.getRequestURI());
			throw new AccessException(AccessException.ACC_BIZ_NOT_FOUND,
					"biz not found,biz code:" + context.getBizCode());
		}
		context.setBiz(biz);
		userSession.setAttribute("content", context.getContentCode());
		String siteCode = context.getEpgSiteContext().getSiteCode();
		playerLogger.info("play|{}|{}|{}|{}|{}|{}", IPUtils.getIpAddr(request), userSession.getUserId(),
				context.getSiteCode(), context.getBizCode(), context.getContentType(), context.getContentCode());

		this.setAccessContext(model, request, context);
		return "/" + siteCode + "/play/play_" + context.getContentType() + ".jsp";
	}
}
